iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 2
6
Modern Web

ElectronJS系列 第 2

[ Day 2 ] - 桌面小圖示(一) - 第一支 electron 應用程式

  • 分享至 

  • xImage
  •  

今天我們開始製作第一支 electron 應用程式

預計會長這樣

環境準備

圖片來源 - ilikesticker.com

下面開始動手製作 ithome-fight

第一步 , 下載專案模板

# 複製 Quick Start 儲存庫
$ git clone https://github.com/electron/electron-quick-start

# 進到儲存庫裡
$ cd electron-quick-start

# 安裝相依的套件並執行
$ npm install && npm start

在 electron-quick-start 資料夾中 , 你會看到以下檔案 :

  • main.js : electron 的主程式 , 可在其中執行 & 使用 Node.js 函式 ( ex : fs.writefile )
  • index.html : 顯示的 html 檔案 , 也是我們擺放貓咪圖片的地方
  • preload.js : 會在 index.html 中使用到的 JS 檔 , 常用於與 main.js 做 IPC 溝通

第二步 , 將可愛的貓咪載入

修改 index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'unsafe-inline'">
</head>
<body>
    <img src="playing-cat-loop.gif">
</body>
</html>

第三步 , 隱藏工具列與標題列 , 並將背景透明化

修改 main.js

new BrowserWindow 時 , 建立參數

  • frame : 標題列不顯示
  • transparent : 背景透明
  • autoHideMenuBar : 工具列不顯示
const mainWindow = new BrowserWindow({
    width: 350,
    height: 350,
    webPreferences: {
        preload: path.join(__dirname, 'preload.js'),
    },
+   frame: false,          // 標題列不顯示
+   transparent: true,     // 背景透明
+   autoHideMenuBar: true  // 工具列不顯示
});

第四步 , 讓圖片可以用滑鼠抓著移動

在 body 標籤上追加 style="-webkit-app-region: drag"

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'unsafe-inline'">
</head>
<body style="-webkit-app-region: drag">
    <img src="playing-cat-loop.gif">
</body>
</html>

第五步 , 讓圖片無法選取

在 body 標籤上追加 style="user-select: none"

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'unsafe-inline'">
</head>
<body style="-webkit-app-region: drag;user-select: none">
    <img src="playing-cat-loop.gif">
</body>
</html>

如果做不太出來 , 可以下載 第 2 天成品 然後用 npm start 看到玩耍的小貓 /images/emoticon/emoticon07.gif

備註

如果想要從 ilikesticker 使用其他小貓 ,
可以用 ezgif.com 將小貓的動作次數改成 0 - 無限次

參考資料

今年小弟第一次參加 `鐵人賽` , 如文章有誤 , 請各位前輩提出指正 , 感謝  <(_ _)>

上一篇
[ Day 1 ] - Electron 簡介
下一篇
[ Day 3 ] - 桌面小圖示(二) - Electron 架構說明
系列文
ElectronJS38
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

2
ShawnL
iT邦新手 1 級 ‧ 2020-09-02 10:25:34

貓貓!!!

貓奴在此感謝你 XD
https://ithelp.ithome.com.tw/upload/images/20200902/20119062jqxHIbcZVs.jpg

看更多先前的回應...收起先前的回應...

貓咪大戰又要開始了嗎XDhttps://ithelp.ithome.com.tw/upload/images/20200902/20103130coi5OVaj0q.jpg
家貓在此!貓奴+1~

Tree iT邦新手 3 級 ‧ 2020-09-02 13:54:15 檢舉

當初選圖片時 , 還好沒選狗狗 /images/emoticon/emoticon16.gif

b8201032 iT邦新手 4 級 ‧ 2022-01-17 11:33:24 檢舉

暈~~~opensoruce 就是這樣
照著教學,硬是不能用,
連安裝都不正常

b8201032 iT邦新手 4 級 ‧ 2022-01-17 11:42:23 檢舉

在linux不正常,在windows 是正常,
只好先在windows 學習,感謝大大分享

Tree iT邦新手 3 級 ‧ 2022-01-18 12:04:23 檢舉

當初只在 windows 上做測試 ! /images/emoticon/emoticon16.gif ,
如果要開發 Linux 的 Electron APP 建議可以上 udemy 的 electron.js 課程

arguskao iT邦新手 4 級 ‧ 2022-12-17 21:34:34 檢舉

那mac可以用嗎?

1
iT邦新手 4 級 ‧ 2020-09-14 21:35:15

請問一下,如果照著做彈圖片出現以下狀況
![https://ithelp.ithome.com.tw/upload/images/20200914/20129536070ZpaiKmV.png]
是參數沒設定好嗎~

以解決~依然感謝此文讓我介面變可愛多了/images/emoticon/emoticon01.gif

看更多先前的回應...收起先前的回應...
Tree iT邦新手 3 級 ‧ 2020-09-14 21:40:22 檢舉

width , height 可調大一點就不會有滾動條出現 /images/emoticon/emoticon07.gif

Tree iT邦新手 3 級 ‧ 2020-09-14 21:44:08 檢舉

或是你可以將 body 的 style 改成 style="-webkit-app-region: drag;user-select: none;overflow:hidden" 讓滾動條不顯示

iT邦新手 4 級 ‧ 2020-09-14 22:51:31 檢舉

謝謝!

Tree iT邦新手 3 級 ‧ 2020-09-16 09:24:38 檢舉

/images/emoticon/emoticon12.gif

我要留言

立即登入留言